/************************************   

DESCRIPTION: The table scripts are in R. We clean up some of the 
	output files before we format the tables in R. We saveold to use the
	relevant R package we had at the time. 
	
	This file specifically adds the Rsquared results in the right spot on the mortality results. 
 
 Datasets Used:
  (1) overall_death_zip_small
  (2) death_lim_small
  (3) death_fe_small

 Datasets Created:
  (1) overall_death_zip_small
  (2) death_lim_small
  (3) death_fe_small

************************************/
clear all
set more off
capture log close
clear all
 
 cd "K:\BE\1257\Projects\Medicare Claims\HospRetro\Drafts\Health_Outcomes\FigureCreation\PSPatients\RefereeResponse\MakeTables"
 

 *Death results
  *zip Overall
	use .\overall_death_zip_small.dta
		gen id = _n

			foreach var of varlist rn* {
				local i = regexr("`var'","rn","")
				di "`i'"
				gen flag`i'=beta_`i'!=. & sesq_`i'==.
				sort flag`i' id
				by flag`i' : replace flag`i'=_n if flag`i'==1
				replace rn`i' = "rsq" if flag`i'==3
			}
		sort id
		drop flag*

	saveold .\overall_death_zip_small.dta, version(12) replace

 *limited specification
	use .\death_lim_small.dta
		capture drop id
		gen id = _n

			foreach var of varlist rn* {
				local i = regexr("`var'","rn","")
				di "`i'"
				gen flag`i'=beta_`i'!=. & sesq_`i'==.
				sort flag`i' id
				by flag`i' : replace flag`i'=_n if flag`i'==1
				replace rn`i' = "rsq" if flag`i'==3
			}
		sort id
		drop flag*

	saveold .\death_lim_small.dta, version(12) replace


*Matched ID FE specification
	use .\death_fe_small.dta
	gen id = _n

		foreach var of varlist rn* {
			local i = regexr("`var'","rn","")
			di "`i'"
			gen flag`i'=beta_`i'!=. & sesq_`i'==.
			sort flag`i' id
			by flag`i' : replace flag`i'=_n if flag`i'==1
			replace rn`i' = "rsq" if flag`i'==3
		}
	sort id
	drop flag*

	saveold .\death_fe_small.dta, version(12) replace
